<?php 
class Structure_ZendFramework {

    private $_applicationName = "Application";
    /**
    * @var Db $db
    */
    private $_db;
    private $_projectId;


    /**
    * Constructure 
    */
    public function __construct($db, $projectId){
        $this->_db = $db;
        $this->_projectId = $projectId;
    }

    /**
    * General project
    * @author tien.nguyen
    */
    public function createProject(){
        if(!file_exists(PATH_PROJECT_GENERAL.'/'.NAME_PROJECT_GENERAL)){
            mkdir(PATH_PROJECT_GENERAL.'/'.NAME_PROJECT_GENERAL, 777);
        }

        full_copy(PATH_STRUCTURE.'/zend-framework/samples', 
        PATH_PROJECT_GENERAL.'/'.NAME_PROJECT_GENERAL);

    }

    /**
    * this is function run for project
    * @author tien.nguyen
    */    
    public function run(){
        echo "\nStart create zend framework.";
        $this->createProject();
        echo "\nStart create controllers.";
        $this->generalControllers();
        echo "\nStart create models.";
        $this->generalModels();
        echo "\nStart create db table models.";        
        $this->generalDbModels();
        echo "\nStart create views.";
        $this->generalViews();
        echo "\nStart create form.";
        $this->generalForms();
        echo "\nStart create navigation.";
        $this->generalNavigation();
        echo "\nFinished";    
    }

    /**
    * General model
    * @author tien.nguyen
    */
    public function generalModels(){        
        require PATH_STRUCTURE.'/zend-framework/Model.php';
        $controllers = $this->_db->getControllers($this->_projectId);
        foreach($controllers as $controller){
            $actions = $this->_db->getActions($controller->controller_id);

            //add param for actions
            foreach($actions as $action){
                $action->params = $this->_db->getParamAction($action->action_id);
                $action->forms = $this->_db->getFormsByActionId($action->action_id);
                foreach($action->forms as $form){
                    $form->query = $this->_db->getQueryId($form->query_id);
                    $form->query['select'] = $this->_db->getQuerySelect($form->query_id);
                    $form->query['table_join'] = $this->_db->getQueryTableJoin($form->query_id);
                    $form->query['where'] = $this->_db->getQueryWhere($form->query_id);
                    $form->query['order'] = $this->_db->getQueryOrder($form->query_id);
                    $form->query['table'] = $this->_db->getQueryTable($form->query_id);
                }
            } 
            //var_dump($actions); die;

            $modelTemplate = new  Zend_Template_Model($controller, $actions);
            $modelTemplate->writeFile();   
        }
    }

    /**
    * General db model
    * @author tien.nguyen
    */
    public function generalDbModels(){
        require PATH_STRUCTURE.'/zend-framework/DbModel.php';
        $tables = $this->_db->getTables();

        //foreach and general table db
        foreach($tables as $table){
            //get query by table name
            $forms = $this->_db->getFormFullByTableName($table->table_name);
            foreach($forms as $form){
                $form->select = $this->_db->getQuerySelect($form->query_id);
                $form->table_join = $this->_db->getQueryTableJoin($form->query_id);
                $form->where = $this->_db->getQueryWhere($form->query_id);
                $form->table = $this->_db->getQueryTable($form->query_id);
            }
            
            //write file db model
            $dbModelTemplate = new Zend_Template_DbModel($table, $forms);
            $dbModelTemplate->writeFile();   
        }
    }

    /**
    * General controller
    * @author tien.nguyen
    */
    public function generalControllers(){
        require PATH_STRUCTURE.'/zend-framework/Controller.php';
        $controllers = $this->_db->getControllers($this->_projectId);
        foreach($controllers as $controller){
            $controller->action_default = $this->_db->getActionDefault($controller->controller_id);
            $actions = $this->_db->getActions($controller->controller_id);

            //add param for actions
            foreach($actions as $action){
                $action->params = $this->_db->getParamAction($action->action_id);
                $action->forms = $this->_db->getFormsByActionId($action->action_id);
            } 

            $controllerTemplate = new  Zend_Template_Controller($controller, $actions);
            $controllerTemplate->writeFile();   
        }
    }

    /**
    * Genneral form
    * @param array $form
    * @param array $elements
    * @author tien.nguyen
    */
    public function generalForms(){
        require PATH_STRUCTURE.'/zend-framework/Form.php';
        $forms = $this->_db->getForms($this->_projectId);

        foreach($forms as $form){
            $elements = $this->_db->getElements($form->form_id);
            $formTemplate = new Zend_Template_Form($form, $elements);
            $formTemplate->writeFile();   
        } 
    }

    /**
    * General view
    * @author tien.nguyen
    */
    public function generalViews(){
        require PATH_STRUCTURE.'/zend-framework/View.php';
        $controllers = $this->_db->getControllers($this->_projectId);

        foreach($controllers as $controller){
            $actions = $this->_db->getActions($controller->controller_id);

            //add form and params
            foreach($actions as $action){
                $action->params = $this->_db->getParamAction($action->action_id);
                $action->forms = $this->_db->getFormsByActionId($action->action_id);
            }

            $controllerTemp = new Zend_Template_View($controller, $actions);
            $controllerTemp->writeFile();   
        } 
    }

    /**
    * General navigation
    * @author tien.nguyen
    */
    public function generalNavigation(){
        require PATH_STRUCTURE.'/zend-framework/Navigation.php';
        $menus = $this->_db->getMenus($this->_projectId);

        foreach($menus as $menu){
            $menu->parent = "";
        } 

        $navigationTemp = new Zend_Template_Navigation($menus);
        $navigationTemp->writeFile();   
    }
}
